﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TESTCommons.JsonHelper;
using TestManage.Infrastructure.CustomAttribute;
using TestModel.SysManage;
using TestService.SysManage;

namespace TestManage.Controllers
{
    [AopActionFilter(true, false)]
    public class SysMenuController : BaseController
    {
        #region 参数
        private readonly ISys_MenuService _sys_MenuService;
        private readonly ISys_FunctionService _sys_Function;
        #endregion

        #region 构造函数
        public SysMenuController(ISys_MenuService sys_MenuService, ISys_FunctionService sys_Function)
        {
            _sys_MenuService = sys_MenuService;
            _sys_Function = sys_Function;
        }
        #endregion


        public override IActionResult Index()
        {
            return View();
        }

        public override IActionResult Info()
        {
            return View();
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="menu_Name"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult TreeJson(string menu_Name, string userName)
        {
            var menuList = _sys_MenuService.QueryMenu(menu_Name, userName).OrderBy(m => m.gid).ToList();
            var functiontreeViewModels = new List<FunctionViewModel>();
            var parenetData = menuList.Where(item => item.Menu_ParentID == null || item.Menu_ParentID.ToString() == "").FirstOrDefault();
            if(parenetData == null)
                return Json(menuList);
            CreateTreeJson(null, "", functiontreeViewModels, menuList);
            return Json(functiontreeViewModels);
        }

        /// <summary>
        /// 获取功能
        /// </summary>
        [HttpGet]
        public ActionResult GetFunction()
        {
            var FunctionList = _sys_Function.QueryFunction(new Hashtable()).OrderBy(m => m.Function_Num).ToList();
            return Json(FunctionList);
        }

        /// <summary>
        /// 获取选择的功能
        /// </summary>
        [HttpGet]
        public ActionResult GetMenuFunction(string mid)
        {
            var MenuFunList = _sys_MenuService.GetMenuFunction(mid);
            return Json(MenuFunList);
        }

        [HttpPost]
        public ActionResult AddMenu(FunctionViewModel model, List<Sys_MenuFunction> functions)
        {
            model.gid = new Guid();
            model.userName = _account.UserName;
            model.createTime = DateTime.Now;
            functions.ForEach(item => { item.createTime = DateTime.Now; item.userName = _account.UserName; item.MenuFunction_MenuID = model.gid; });
            _sys_MenuService.AddMenu(model, functions);
            return Json(new AjaxResult { type = ResultType.success, message = "添加成功" });
        }

        [HttpPost]
        public ActionResult UpdateMenu(FunctionViewModel model, List<Sys_MenuFunction> functions)
        {
            var entity = _sys_MenuService.GetModel(model.gid);
            if (entity == null)
            {
                return Json(new AjaxResult { type = ResultType.error, message = "已经删除" });
            }
            model.userName = _account.UserName;
            model.createTime = DateTime.Now;
            functions.ForEach(item => { item.createTime = DateTime.Now; item.userName = _account.UserName; item.MenuFunction_MenuID = model.gid; });
            _sys_MenuService.UpdateMenu(model, functions);
            return Json(new AjaxResult { type = ResultType.success, message = "修改成功" });
        }
        
        [HttpPost]
        public ActionResult DeleteMeun(FunctionViewModel model)
        {
            _sys_MenuService.DeleteMenu(model);
            return Json(new AjaxResult { type = ResultType.success, message = "删除成功" });
        }

        [NonAction]
        private void CreateTreeJson(Guid? Menu_ParentID, string menu_ParentName, List<FunctionViewModel> functiontreeViewModels, List<Sys_Menu> childs) //treeview 集合
        {
            var temp = childs.Where(item => item.Menu_ParentID == Menu_ParentID).Select(item => new FunctionViewModel
            {
                gid=item.gid,
                Menu_Num = item.Menu_Num,
                Menu_Name = item.Menu_Name,
                Menu_Url = item.Menu_Url,
                Menu_Icon = item.Menu_Icon,
                Menu_ParentID = item.Menu_ParentID,
                menu_ParentName = menu_ParentName,
                Menu_IsShow = item.Menu_IsShow,
                userName = item.userName,
                createTime = item.createTime
            }).ToList();
            foreach (var tempitem in temp)
            {
                tempitem.children = new List<FunctionViewModel>();
                CreateTreeJson(tempitem.gid, tempitem.Menu_Name, tempitem.children, childs);//在遍历父级以下节点
                functiontreeViewModels.Add(tempitem);//把节点加入集合
            }
        }
    }
}
